package com.imooc.scala.log

import com.ggstar.util.ip.IpHelper
import org.apache.spark.sql.Row
import org.apache.spark.sql.types.{LongType, StringType, StructField, StructType}
import com.ggstar.util.ip.IpHelper

object AccessConvertUtil {

  val struct = StructType(
    Array(
      StructField("url", StringType),
      StructField("traffic", LongType),
      StructField("ip", StringType),
      StructField("city", StringType),
      StructField("time", StringType),
      StructField("day", StringType)
    )
  )

  def parseLog(log: String) = {
    try {
      val splits = log.split("\t")
      val url = splits(1)
      val traffic = splits(2).toLong
      val ip = splits(3)
      val city = IpHelper.findRegionByIp(ip)
      val time = splits(0)
      val day = time.substring(0, 10).replaceAll("-", "")
      Row(url, traffic, ip, city, time, day)
    } catch {
      case e: Exception => Row("", 0L, "", "", "", "")
    }
  }

  def main(args: Array[String]): Unit = {
    val ip = "58.30.15.255"
    val region = IpHelper.findRegionByIp(ip)
    System.out.println(region)
  }
}
